package com.xiaolin.ssosystem.config;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.StandardCharsets;

/**
 * @author xiaolin
 * @description 描述
 * @since 2024/10/17
 */
@Configuration
public class BloomFilterConfig {

    @Bean
    public BloomFilter<String> blackIpBloomFilter() {
        // 预期插入的元素数量
        int expectedInsertions = 1000;
        // 期望的误判率
        double fprobability = 0.001;
        // 创建布隆过滤器
        BloomFilter<String> bloomFilter = BloomFilter.create(
                Funnels.stringFunnel(StandardCharsets.UTF_8),
                expectedInsertions,
                fprobability
        );
        bloomFilter.put("127.0.0.1");
        return bloomFilter;
    }

    @Bean
    public BloomFilter<String> whiteIpBloomFilter() {
        // 预期插入的元素数量
        int expectedInsertions = 1000;
        // 期望的误判率
        double fprobability = 0.001;
        // 创建布隆过滤器
        BloomFilter<String> bloomFilter = BloomFilter.create(
                Funnels.stringFunnel(StandardCharsets.UTF_8),
                expectedInsertions,
                fprobability
        );
        bloomFilter.put("127.0.0.1");
        return bloomFilter;
    }
}
